package com.findviewbydream.evil.helloyouth.SlideMenu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.helloyouth.R;
import com.findviewbydream.evil.helloyouth.Utils.SuperToast.SuperToast;

import java.util.ArrayList;

import cn.iwgang.familiarrecyclerview.FamiliarRecyclerView;
import de.hdodenhof.circleimageview.CircleImageView;

/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth.SlideMenu
 * 时间：16-6-10 上午12:13
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：主界面右侧菜单Fragment
 ********************************************/
public class RightFragment extends Fragment
{
    private View view;
    private static RightFragment rightFragment;

    private ImageView backgroud;
    private TextView news;
    private ImageView newsImg;
    private FamiliarRecyclerView recyclerView;

    private Adapter adapter;
    private ArrayList<ItemBean> list;
    private LinearLayout delete_linearLayout;
    private ImageButton delete;

    public static RightFragment newInstance(Bundle b)
    {
        if (rightFragment == null)
            rightFragment = new RightFragment();
        rightFragment.setArguments(b);
        return rightFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.right_fragment, container, false);
        initView(view);/*初始化控件*/

        list = new ArrayList<>();

        for (int i = 0; i < 20; i++)
        {
            ItemBean itemBean = new ItemBean();
            itemBean.content = i + "位置";
            list.add(itemBean);
        }

        initData();/*初始化数据*/
        setRecyclerView();/*设置RecyclerView*/
        onClick();/*点击事件*/

        return view;
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view)
    {
        backgroud = (ImageView) view.findViewById(R.id.backgroud);
        news = (TextView) view.findViewById(R.id.news);
        recyclerView = (FamiliarRecyclerView) view.findViewById(R.id.recyclerView);
        newsImg = (ImageView) view.findViewById(R.id.newsImg);
        delete_linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout2);
        delete = (ImageButton) view.findViewById(R.id.delete);
    }

    /**
     * 初始化数据
     */
    private void initData()
    {
        Glide.with(getContext()).load(R.drawable.slidebar_right_background).into(backgroud);
        Glide.with(getContext()).load(R.drawable.news).into(newsImg);
    }

    /**
     * 设置RecyclerView
     */
    private void setRecyclerView()
    {

        adapter = new Adapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        /**
         * 滑动删除
         *
         * TODO:暂停使用，有严重bug
         */

        /*SwipeDismissRecyclerViewTouchListener listener =
                new SwipeDismissRecyclerViewTouchListener.Builder(recyclerView,
                        new SwipeDismissRecyclerViewTouchListener.DismissCallbacks()
                        {
                            @Override
                            public boolean canDismiss(int i)
                            {
                                list.remove(i);
                                adapter.notifyItemRemoved(i);
                                return true;
                            }

                            @Override
                            public void onDismiss(View view)
                            {

                            }
                        })
                        .setIsVertical(false).create();*/
        //recyclerView.setOnTouchListener(listener);
    }

    /**
     * 点击事件
     */
    private void onClick()
    {
        delete_linearLayout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

            }
        });
        delete.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                list.clear();
                adapter.notifyDataSetChanged();
            }
        });
    }


    /**
     * RecyclerView的Adapter
     */
    private class Adapter extends RecyclerView.Adapter<ViewHolder>
    {
        ItemBean itemBean;

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.right_fragment_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position)
        {
            itemBean = list.get(position);
            holder.content.setText(itemBean.content);
        }

        @Override
        public int getItemCount()
        {
            return list.size();
        }
    }


    private class ViewHolder extends RecyclerView.ViewHolder
    {
        private CircleImageView circleImageView;
        private ImageView imageView;
        private TextView time;
        private TextView content;

        public ViewHolder(View itemView)
        {
            super(itemView);
            this.circleImageView = (CircleImageView) itemView.findViewById(R.id.circleImageView);
            this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
            this.time = (TextView) itemView.findViewById(R.id.time);
            this.content = (TextView) itemView.findViewById(R.id.content);
        }
    }

    private class ItemBean
    {
        private String time;
        private String content;
    }


}
